import type { App } from 'vue';
import {
    getChannelList,
    getChannelNameById,
    getChannelIdByName,
    clearChannelList,
    getChannelListSync,
    getChannelNameByIdSync,
    getChannelIdByNameSync,
    updateChannelList,
    type ChannelItem
} from '@/utils/channel';

// 声明全局属性类型
declare module '@vue/runtime-core' {
    interface ComponentCustomProperties {
        $getChannelList: () => ChannelItem[];
        $getChannelListAsync: () => Promise<ChannelItem[]>;
        $getChannelNameById: (channelId: string) => string | null;
        $getChannelNameByIdAsync: (channelId: string) => Promise<string | null>;
        $getChannelIdByName: (channelName: string) => string | null;
        $getChannelIdByNameAsync: (channelName: string) => Promise<string | null>;
        $clearChannelList: () => void;
        $refreshChannelList: () => Promise<ChannelItem[]>;
    }
}

export default {
    install(app: App) {
        // 同步方法（从缓存获取，用于模板）
        app.config.globalProperties.$getChannelList = getChannelListSync;
        app.config.globalProperties.$getChannelNameById = getChannelNameByIdSync;
        app.config.globalProperties.$getChannelIdByName = getChannelIdByNameSync;

        // 异步方法（用于脚本中预加载）
        app.config.globalProperties.$getChannelListAsync = getChannelList;
        app.config.globalProperties.$getChannelNameByIdAsync = getChannelNameById;
        app.config.globalProperties.$getChannelIdByNameAsync = getChannelIdByName;

        // 清空渠道缓存
        app.config.globalProperties.$clearChannelList = clearChannelList;

        // 刷新渠道数据
        app.config.globalProperties.$refreshChannelList = updateChannelList;
    }
}; 